﻿using AutoMapper;
using B.S.Packaged.Domain;
using B.S.Packaged.ErrorCode;
using B.S.Packaged.WebAPI.Write.Applications.Commands.CompletedfinishedCommand;
using MediatR;
using SqlSugar;

namespace B.S.Packaged.WebAPI.Write.Applications.CommandHandlers.CompletedfinishedHandler
{
    public class AddCompletedfinishedHandler : IRequestHandler<AddCompletedfinishedCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public AddCompletedfinishedHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<object>> Handle(AddCompletedfinishedCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<Completedfinished>(request);
            string yeartime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string ProductCodess1 = "CPZJ" + yeartime;
            model.inspection_report_number = ProductCodess1;

            model.IsDel = false;

            result.data = await db.Insertable(model).ExecuteCommandAsync();
            if (result.data != null)
            {
                result.code = APIResultCode.success;
                result.msg = "包装成品完工添加成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "包装成品完工添加失败";
            }
            return result;
        }
    }
}
